package org.jground.rx.ui.zk.viewControl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RXAttribute;
import org.jground.rx.RXObjectService;
import org.jground.rx.RXTypeRef;
import org.jground.rx.RXView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.UIResultList;
import org.jground.util.ArrayUtils;

public class UIAttributeViewRef extends UIAttributeViewBase {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Log logger = LogFactory.getLog(UIAttributeViewRef.class);

	public UIAttributeViewRef createNew(UIFrame uiFrame, RXView moControl) {
		return new UIAttributeViewRef(uiFrame, moControl);
	}

	//
	public UIAttributeViewRef() {
	}

	private RXAttribute moAttribute;

	private UIResultList resultList;

	private UIAttributeViewRef(UIFrame uiFrame, RXView moControl) {
		super(uiFrame, moControl);
		this.moAttribute = moControl.getRXAttribute();
		if (moAttribute.getRXType().getBaseType() instanceof RXTypeRef) {
			resultList = new UIResultList(uiFrame);
			super.add(UIResultList.uiBox(resultList));
		} else {
			logger.error("Unexpected RXType. Ref expected, got "
					+ moAttribute.getRXType().getBaseType());
		}
	}

	public void set(String... values) {
		clear();
		if (ArrayUtils.isNotEmpty(values)) {
			List<IRXObject> rxds = RXObjectService.getInstance().getByIds(
					values);
			resultList.addObjects(rxds);
			for (IRXObject rxd : rxds) {
				renderToStringS.add(uiFrame.getUISynopsisRendererFactory()
						.toString(rxd));
			}
		}
	}

	public void clear() {
		resultList.removeAllObject();
		renderToStringS.clear();
	}

}